﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Diagnostics;
using System.Threading;

namespace RNT.Common.Utils
{
    /// <summary>
    /// 字符操作辅助类
    /// </summary>
    public class StringUtil
    {
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="target">预处理字符</param>
        /// <param name="length">预截取的长度</param>
        /// <returns>如果满足截取条件返回截取后的字符,否则不截取</returns>
        public static string CutString(string target, int length)
        {
            if (target.Length > length)
            {
                return target.Substring(0, length);
            }
            else
            {
                return target;
            }
        }

        /// <summary>
        /// 转全角函数
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        public static string ToSBC(string input)
        {
            //全角空格为12288，半角空格为32
            //其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248

            char[] cher = input.ToCharArray();
            for (int i = 0; i < cher.Length; i++)
            {
                if (cher[i] == 32)
                {
                    cher[i] = (char)12288;
                    continue;
                }
                if (cher[i] < 127)
                {
                    cher[i] = (char)(cher[i] + 65248);
                }
            }
            return new String(cher);
        }

        /// <summary>
        /// 转半角函数
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToDBC(string input)
        {
            char[] cher = input.ToCharArray();
            for (int i = 0; i < cher.Length; i++)
            {
                if (cher[i] == 12288)
                {
                    cher[i] = (char)32;
                    continue;
                }
                if (cher[i] > 65280 && cher[i] < 65375)
                {
                    cher[i] = (char)(cher[i] - 65248);
                }
            }
            return new String(cher);
        }

        ///// <summary>
        ///// 获取当前行号
        ///// </summary>
        ///// <returns></returns>
        //public static int GetLineNum()
        //{
        //    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
        //    return st.GetFrame(0).GetFileLineNumber();
        //}

        static Random rnd = new Random((int)DateTime.Now.Ticks);
        /// <summary>
        /// 获取依据时间生成的编号(21位)
        /// </summary>
        /// <returns></returns>
        public static String GetDateGeneratorID()
        {
            Thread.Sleep(100);
            DateTime now = DateTime.Now;
            string dateString = now.ToString("yyyyMMddHHmmss");//14
            string msecond = String.Format("{0:000}", now.Millisecond);//3
            string rstring = String.Format("{0:0000}", rnd.Next(10000));//4

            return String.Format("{0}{1}{2}", dateString, msecond, rstring);
        }


        #region 用C#获取当前函数名，当前代码行，源代码文件

        public static void Test()
        {

            StackTrace st = new StackTrace(new StackFrame(true));
            Console.WriteLine(" Stack trace for current level: {0}", st.ToString());

            StackFrame sf = st.GetFrame(0);

            Console.WriteLine(" File: {0}", sf.GetFileName());

            Console.WriteLine(" Method: {0}", sf.GetMethod().Name);

            Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber()); Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());

        }

        #endregion

    }
}
